package com.txhy.certificatechain.presenter.home;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.txhy.certificatechain.api.UrlAddress;
import com.txhy.certificatechain.presenter.base.BasePresenter;
import com.txhy.certificatechain.ui.view.index.IndexView;
import com.txhy.certificatechain.utils.GsonUtil;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: HomePresenter.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 首页
 * 创建时间: 2018/9/20 9:06
 * 更新时间：2018/9/20 9:06
 * -----------------------------------------------------------------
 * 2018/9/20 9:06: 创建 HomePresenter.java (侯建军);
 * -----------------------------------------------------------------
 */
public class HomePresenter extends BasePresenter<IndexView> {

    public void getBannerData(final String flag) {
        OkGo.<String>post(UrlAddress.GETLB_URL)//
                .tag(this)//
                .params("flag", flag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)  //使用缓存
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            if (flag.equals("webLb")) {
                                getView().getBannerSuccess(response.body());
                            } else if (flag.equals("appMallLb")) {
                                getView().getShopBannerSuccess(response.body());
                            }

                        } else {
                            getView().getBannerFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                        getView().getBannerFail("请求失败请检查网络");
                    }
                });
    }

    public void getNewsData(String type) {
        OkGo.<String>post(UrlAddress.LIST_URL)//
                .tag(this)//
                .params("type", type)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)  //使用缓存
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().getNewsSuccess(response.body());
                        } else {
                            getView().getNewsFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                        getView().getNewsFail("请求失败请检查网络");
                    }
                });
    }
}
